package com.waterflow.xanner;

import android.animation.Animator;
import android.animation.Animator.AnimatorListener;
import android.animation.ObjectAnimator;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.view.animation.Interpolator;
import android.widget.Button;
import android.widget.TextView;

public class TutorialActivity extends Activity {

	ObjectAnimator animator = null;

	private static final int COVERS_ANIMATION_LENGTH = 1000;

	private boolean canProcceed = true;

	private Runnable allowProgress = new Runnable() {

		@Override
		public void run() {
			canProcceed = true;
		}
	};

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		// Inflate XML layout
		setContentView(R.layout.activity_tutorial);

		final View view = findViewById(R.id.puzzle_object_text);

		view.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
		view.getViewTreeObserver().addOnGlobalLayoutListener(
				new OnGlobalLayoutListener() {
					@Override
					public void onGlobalLayout() {
						startAnimation();
					}
				});
	}

	private void startAnimation() {
		if (animator != null) {
			animator.cancel();
		}

		View view = findViewById(R.id.puzzle_object);

		float height = view.getHeight();

		animator = ObjectAnimator.ofFloat(view, "translationY", height / 2,
				-height / 2);

		animator.setDuration(5000);

		animator.setRepeatCount(ObjectAnimator.INFINITE);

		animator.setInterpolator(new Interpolator() {

			@Override
			public float getInterpolation(float input) {
				return input;
			}
		});

		animator.start();
	}

	private int tutorial_stage = 0;

	public void onButtonClick(View view) {
		if (!canProcceed) {
			return;
		}

		tutorial_stage++;
		canProcceed = false;
		TextView text = (TextView) findViewById(R.id.tutorial_text);
		((Button)view).setText("Continue");
		
		switch (tutorial_stage) {
		case 1:
			
			text.setText(R.string.tutorial_text_2);
			canProcceed = true;
			break;
		case 2:
			openCovers();
			text.setText(R.string.tutorial_text_3);
			canProcceed = true;
			break;
		case 3:
			closeCovers();
			text.setText(R.string.tutorial_text_4);
			canProcceed = true;
			break;
		case 4:
			text.setText(R.string.tutorial_text_5);
			canProcceed = true;
			break;
		case 5:
			text.setText(R.string.tutorial_text_6);
			((Button)view).setText("Play!");
			canProcceed = true;
			break;
		case 6:
			canProcceed = false;
			finish();
			break;
		}
	}

	private void openCovers() {
		float startThickness = getResources().getDimension(
				R.dimen.slit_thickness);
		float endThickness = getResources().getDimension(
				R.dimen.slit_thickness_answer_revealed);

		animateSlit(startThickness, endThickness, allowProgress);
	}

	private void closeCovers() {
		float startThickness = getResources().getDimension(
				R.dimen.slit_thickness);
		float endThickness = getResources().getDimension(
				R.dimen.slit_thickness_answer_revealed);

		animateSlit(endThickness, startThickness, allowProgress);
	}

	private void animateSlit(float start, float end, final Runnable onEnd) {
		final View slit = findViewById(R.id.slit_window);

		final ViewGroup.LayoutParams params = slit.getLayoutParams();

		Object modifier = new Object() {
			@SuppressWarnings("unused")
			public void setHeight(float height) {
				params.height = (int) height;
				slit.setLayoutParams(params);
			}
		};

		ObjectAnimator animatorSlit = ObjectAnimator.ofFloat(modifier,
				"Height", start, end);
		animatorSlit.setDuration(COVERS_ANIMATION_LENGTH);
		if (onEnd != null) {
			animatorSlit.addListener(new AnimatorListener() {

				@Override
				public void onAnimationStart(Animator animation) {
				}

				@Override
				public void onAnimationRepeat(Animator animation) {
				}

				@Override
				public void onAnimationEnd(Animator animation) {
					onEnd.run();
				}

				@Override
				public void onAnimationCancel(Animator animation) {
					onEnd.run();
				}
			});
		}

		animatorSlit.start();
	}
}
